package com.jeecms.wicool.dao.activities.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Transaction;

import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.activities.ActivitiesDao;
import com.jeecms.wicool.entity.activities.Activities;

public class ActivitiesDaoImpl extends HibernateBaseDao<Activities, Integer> implements ActivitiesDao {

	@Override
	public Pagination getPage(String title, Date publishTime, int pageNo,
			int pageSize) {
		// TODO Auto-generated method stub
		Finder f = Finder.create("select bean from Activities bean where 1=1 ");
		if (!StringUtils.isBlank(title)){
			f.append(" and bean.title like :title");
			f.setParam("title", "%" + title + "%");
		}
		if(publishTime!=null){
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			SimpleDateFormat simpleTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			
			String start = dateFormat.format(publishTime)+" 00:00:00";
			String end = dateFormat.format(publishTime)+" 23:59:59";
			
			f.append(" and bean.publishTime >= :start and bean.publishTime <= :end");
			try {
				f.setParam("start", simpleTime.parse(start));
				f.setParam("end", simpleTime.parse(end));
			} catch (ParseException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
			
		}
		
		f.append(" order by bean.publishTime desc");
		return find(f, pageNo, pageSize);
		
	}

	@Override
	public Activities findById(Integer id) {
		// TODO Auto-generated method stub
		Activities entity = get(id);
		return entity;
	}

	@Override
	public Activities save(Activities bean) {
		// TODO Auto-generated method stub
		getSession().save(bean);
		return bean;
	}

	@Override
	public Activities deleteById(Integer id) {
		// TODO Auto-generated method stub
		Activities entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	
	
	@Override
	public ArrayList<Activities> deleteByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		if (null ==ids || ids.length ==0) {
			return null;
		}
		ArrayList<Activities> arry = new ArrayList<Activities>();
		
		Transaction tx = getSession().beginTransaction();
		for (int i = 0; i < ids.length; i++) {
			arry.add(deleteById(ids[i]));
			
		}
		getSession().flush();
		getSession().clear();
		tx.commit();
		return arry;
	}

	@Override
	public List<Activities> getAll(Activities activities) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Class<Activities> getEntityClass() {
		// TODO Auto-generated method stub
		return Activities.class;
	}

	@Override
	public Activities update(Activities entity) {
		// TODO Auto-generated method stub
		if (entity != null) {
			getSession().update(entity);
			getSession().flush();
			getSession().clear();
		}
		return entity;
	}

	
}
